<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>号卡销售前端轮询逻辑</title>
    <script src="js/jquery.min.js"></script>
</head>

<body>
    <h3>号卡销售前端轮询逻辑</h3>
    <div id="time"></div>
    <div id="result"></div>
</body>
<script>
    var orderTime = 0;
    var orderTimeLimit = 10; //轮询1分钟
    var orderTimeLimit2 = 20;//轮询5分钟
    var orderInterval = setInterval("checkOrder()",5000);
    var defer = $.Deferred();

    var ajaxArr = [];

    function checkOrder() {
        //console.log("定时进来:" + orderTime);
        //内层还是一个Ajax异步请求
        orderTime = orderTime+1;
        //找个只是页面显示
        $("#time").text(orderTime);
        //ajaxOrder();
        ajaxArr.push(new ajaxOrderObj());
        if(ajaxArr.length>1){
            for (var i=0;i<ajaxArr.length;i++){
                var ajaxObj = ajaxArr[i];
                var readyState = ajaxObj.ajax.readyState;
                var statusText = ajaxObj.ajax.statusText;

                if((statusText=="timeout" && readyState==0) || readyState==4){
                    continue;
                }
                if(ajaxObj.ajax.readyState == 1){
                    break;
                }
                //判断前面的请求如果在执行，不执行
                if(ajaxObj.ajax.readyState == undefined){
                    ajaxObj.startAjax();
                    break;
                }
            }
        }

    }

    function ajaxOrderObj(){
        var obj = new Object();
        obj.ajax = {};
        obj.startAjax = function () {
            this.ajax = ajaxOrder();
        }
        return obj;
    }

    function ajaxOrder(){

        return $.ajax({
            url: 'http://127.0.0.1:8080/info',
            type: "post",
            dataType: "json",
            async: true,
            data:"",
            timeout:4000,
            success:function(data){
                dealOrder(data);
                defer.resolve(data);
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){

            },
            complete : function(XMLHttpRequest,status){ //请求完成后最终执行参数
                if(status=='timeout'){//超时,status还有success,error等值的情况
                    //ajaxTimeoutTest.abort();
                    console.log("超时---------------------");
                }
            }
        });

    }

    function dealOrder(data){
        var code = data.code;
        var endFlag = true;
        if(code == 0){
            clearInterval(orderInterval);
            $("#result").text("订单受理成功");
        }
        if(orderTime >= orderTimeLimit && true){
            //定时结束
            //clearInterval(orderInterval);
            //$("#result").text("1分钟轮询过了，订单受理失败");
        }
        if((orderTime >= orderTimeLimit2) && true){
            //定时结束
            //clearInterval(orderInterval);
            //$("#result").text("5分钟轮询过了，订单受理失败");
        }
    }

    //案例
    function ajaxOrderCheck(data){
        $.ajax({
            url: 'http://127.0.0.1:8080/info',
            type: "post",
            dataType: "json",
            async: true,
            data:data,
            success:function(data){

            },
            beforeSend:function(XMLHttpRequest){

            },
            complete:function(XMLHttpRequest,textStatus){

            },
            error:function(XMLHttpRequest, textStatus, errorThrown){

            }
        });
    }

    var ajax2 = ajaxTest();

    function ajaxTest(data){
        return $.ajax({
            url: 'http://127.0.0.1:8080/info',
            type: "post",
            dataType: "json",
            async: true,
            data:"",
            timeout:3000,
            success:function(data){
                dealOrder(data);
                defer.resolve(data);
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){

            }
        });
    }
</script>
</html>